﻿using KinonekoSoftware.CodariaDB.Documents.Index;
using KinonekoSoftware.Extensions.Collections;

namespace KinonekoSoftware.CodariaDB.Documents.Composites
{
    partial class CompositeEngine
    {
        public IEnumerable<AbilityBundle> GetAbilityBundles(InstanceCacheEngine engine)
        {
            var bundles = AbilityBundleDB.FindAll();
            var list    = new List<AbilityBundle>(32);

            foreach (var bundle in bundles)
            {
                bundle.Items     ??= new ObservableCollection<string>();
                bundle.Abilities ??= new ObservableCollection<InstanceCache>();
                
                //
                //
                bundle.Abilities.AddMany(bundle.Items.Select(x => engine.Get(InstanceType.Ability, x)), true);
                list.Add(bundle);
            }

            return list;
        }


        public void AddAbilityBundle(AbilityBundle bundle)
        {
            if (bundle is null)
            {
                return;
            }

            //
            //
            bundle.Items ??= new ObservableCollection<string>();
            
            //
            //
            AbilityBundleDB.Insert(bundle);
        }
        
        public void UpdateAbilityBundle(AbilityBundle bundle)
        {
            if (bundle is null)
            {
                return;
            }

            //
            //
            bundle.Items ??= new ObservableCollection<string>();
            
            //
            //
            AbilityBundleDB.Update(bundle);
        }
        
        public void RemoveAbilityBundle(AbilityBundle bundle)
        {
            if (bundle is null)
            {
                return;
            }
            
            //
            //
            AbilityBundleDB.Delete(bundle.Id);
        }
    }
}